package com.xyh.student.service.impl;

import com.xyh.student.dao.CourseDao;
import com.xyh.student.entity.Course;
import com.xyh.student.service.CourseService;
import com.xyh.student.entity.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("courseService")
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;

    @Override
    public ResponseData getCourses() {
        try{
            List<Course> courses=courseDao.getCourses();
            return new ResponseData("0","请求成功",courses);
        }catch(Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData updateCourse(Course course) {
        try{
            this.courseDao.update(course);
            List<Course> courses=courseDao.getCourses();
            return new ResponseData("0","请求成功",courses);
        }catch(Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData insertCourse(Course course) {
        try{
            this.courseDao.insert(course);
            List<Course> courses=courseDao.getCourses();
            return new ResponseData("0","请求成功",courses);
        }catch(Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData deleteByName(String name) {
        try{
            this.courseDao.deleteByName(name);
            List<Course> courses=courseDao.getCourses();
            return new ResponseData("0","请求成功",courses);
        }catch(Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData deletesByNames(String[] names) {
        try{
            this.courseDao.deletesByNames(names);
            List<Course> courses=courseDao.getCourses();
            return new ResponseData("0","请求成功",courses);
        }catch(Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData numberOfCourse() {
        try{
            int i=this.courseDao.numberOfCourse();
            return new ResponseData("0","请求成功",i);
        }catch(Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData queryByName(String name) {
        try{
            name = "%"+name+"%";
            List<Course> courses=this.courseDao.queryByName(name);
            return new ResponseData("0","请求成功",courses);
        }catch(Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData findByPage(Integer page, Integer size) {
        try{
            List<Course> courses=this.courseDao.findByPage((page-1)*size,size);
            return new ResponseData("0","请求成功",courses);
        }catch(Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData deleteById(Integer id) {
        try {
            this.courseDao.deleteById(id);
            List<Course> courses=courseDao.getCourses();
            return new ResponseData("0","请求成功",courses);
        }catch(Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData deletesById(int[] ids) {
        try {
            this.courseDao.deletes(ids);
            List<Course> courses=courseDao.getCourses();
            return new ResponseData("0","请求成功",courses);
        }catch(Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

}
